import { excludePath } from '../../router'
import { appState } from '../manager/appManager'
import { hasOwnProp, travelTree } from './normalUtils'


export function encodeSystemString(value:string) {
    if (!value) return value
     return value.replace(/\//g, 'F')
 }

 export function getSystemByEncodeValue(arr:any[], value:string, att = 'name') {
    for (const item of arr) {
        const _value = encodeSystemString(item[att] || '')
        if (value === _value) return item
    }
    return null
 }

/**
 * 验证菜单是不是自己系统的菜单 兼容以前的判断方法 后端代码同步后再修改 temp???
 * @param menu
 */
 export function validSystemMenuIsSelf(menu:any) {
    return ((menu.systemId && menu.systemId === appState.code) ||
        (!hasOwnProp(menu, 'systemId') && String(menu.isFrame) !== '0'))
}

/**
 * 通过路由获取主菜单
 * @param path
 */
export function getPathMenuRoot(path:string) {
    const list = appState.menuList
    let root:any = null
    let main:any = null
    travelTree(list, (item: any, pItem: any) => {
        if (!pItem) main = item
        if (item.path === path) {
            root = main
            return -1
        }
    })
    return root
}
